#include "../include/head.h"

int tcpInit(int *sockFd,char *ip, char *port)
{
    //使sockFd成为socket
    *sockFd = socket(AF_INET, SOCK_STREAM, 0);
    //初始化TCP监听套接字，等待客户机的连接请求
    int ret = 0;
    struct sockaddr_in serAddr;
    memset(&serAddr, 0, sizeof(serAddr));
    
    serAddr.sin_family = AF_INET;
    serAddr.sin_addr.s_addr = inet_addr(ip);
    serAddr.sin_port = htons(atoi(port));

    //设置服务器端的ip和port可重用
    int reuse = 1;
    setsockopt(*sockFd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

    //将sockFd和服务端的ip和port关联起来
    ret = bind(*sockFd, (struct sockaddr*)&serAddr, sizeof(serAddr));
    ERROR_CHECK(ret, -1, "bind");

    //等待客户机连接
    ret = listen(*sockFd, 10);
    ERROR_CHECK(ret, -1, "listen");

    return 0;
}

